package com.jdbc.conn;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

/**
 * @author wzm
 * @create 2023-02-21 15:23
 */
//连接mysql二 读取配置文件
public class Conn2 {
    public static void main(String[] args){


        try {
            //方式一 使用类加载器以流的形式读取文件到java中 仅适用于测试
            //InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties");

            //通过当前类获取的类加载读取文件
            InputStream is = Conn2.class.getClassLoader().getResourceAsStream("jdbc.properties");
            //读取properties文件
            Properties properties = new Properties();
            //将输入流中的内容读取到properties对象里
            properties.load(is);

            //从properties对象中按照key却出需要的value
            String user = properties.getProperty("user");
            String url = properties.getProperty("url");
            String password = properties.getProperty("password");
            String driverClass = properties.getProperty("driverClass");


            Class.forName(driverClass);
            Connection connection = DriverManager.getConnection(url,user,password);
            if (null != connection){
                System.out.println("打开与mysql的连接成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("打开连接失败");
        }

    }
}
